package ws.very.util.aws.cloudsearch

import scala.collection.convert.WrapAsJava
import aws.cloudsearch.AwsCSAddRequest
import aws.cloudsearch.AwsCSDocumentService

trait AwsCSUpload extends WrapAsJava {
  def docEndPoint: S
  val uploadService = new AwsCSDocumentService(docEndPoint)
  def map2AddReq(id: S, version: Long = System.currentTimeMillis() / 1000)(elems: (S, Any)*) = {
    val addRequest = new AwsCSAddRequest
    addRequest.id = id //不支持中文 搜索 FE84C613C17 找不出
    addRequest.version = version
    elems.foreach {
      case (k, v) =>
        v match {
          case i: I => addRequest.addField(k, i)
          case s: S => addRequest.addField(k, s)
          case it: Iterable[S] => addRequest.addField(k, it.toBuffer)
        }
    }
    addRequest
  }

  def +(req: AwsCSAddRequest*) =
    uploadService.addDocuments(req)
  def +(req: Iterable[AwsCSAddRequest]) = uploadService.addDocuments(req.toList)
}